package com.gdit.zax.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.UserDao;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.MD5;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;
import com.gdit.zax.utils.webUtil;

public class Login extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//http://pingkgs3pc:8080/zuiaixue/login?username=ysp&password=e10adc3949ba59abbe56e057f20f883e&sid=-1&device=android&system=4.1
		//http://pingkgs3pc:8080/zuiaixue/login?username=0104120337&password=033ac07144fefdebf21aa5d73653b3f2&schoolid=1&device=android&system=4.1
//		webUtil.printNetData(request);
		
		String username = request.getParameter("username");
		String password =  MD5.getMD5(request.getParameter("password"));//加了一次md5存入数据库
		int sid = TextUtil.String2int(request.getParameter("schoolid"), -1);
		String device = request.getParameter("device");
		String system = request.getParameter("system");
		
		if (TextUtil.isEmpty(username)) {
			O.printFail(response, 1, "请输入用户名!");
			return;
		}
		if (TextUtil.isEmpty(password)) {
			O.printFail(response, 1, "请输入密码!");
			return;
		}
		if (sid == -1) {
			O.printFail(response, 1, "请选择学校!");
			return;
		}
		
		
		Connection conn = DBUtil.getConnection();
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}
		
		DAOFactory dao  = DAOFactory.init(conn, request, response);
		UserDao udao =dao.getUserDao();
		if (!udao.isRegister(sid, username)){
			O.printFail(response, 1, "账号错误!");
			return;
		}
		
		UserInfo info = udao.login(sid, username, password, system, device, Constant.LOGINTYPE_APP);
		if (info==null) {
			O.printFail(response, 1, "密码错误!");
			return;
		}
		
		
		
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("userid", info.getId());
		data.put("accesstoken", info.getAccesstoken());
		
		Map<String, Object> userInfo = new HashMap<String, Object>();
		userInfo.put("nick", info.getNick());
		userInfo.put("username", info.getUsername());
		userInfo.put("pic", webUtil.getWebAbsoluteUrl(info.getUserhead(), Constant.PIC_userhead));
		userInfo.put("content", info.getIntroduction());
		userInfo.put("sex", info.getSex());
		userInfo.put("age", info.getAge());
		userInfo.put("regtime", info.getAddtime());
		userInfo.put("logintime", info.getLogintime());
		userInfo.put("truename", info.getTruename());
		userInfo.put("email", info.getEmail());
		userInfo.put("phone", info.getPhone());
		userInfo.put("score", info.getScore());
		userInfo.put("introduction", info.getIntroduction());
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("errorcode", 0);
		map.put("data", data);
		map.put("userInfo", userInfo);
		map.put("message", "success");
		O.print(response, map);
		
		
//		username	String	登陆名
//		password	String	
//		schoolid	int	学习id
//		imei	string	可选
//		device	string	可空，用于统计
//		system	string	可空，用于统计

	}

}
